package com.briup.web.servlet;

import com.briup.bean.Customer;
import com.briup.bean.ShopCar;
import com.briup.service.IShopCarService;
import com.briup.service.impl.ShopCarServiceImpl;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.util.List;

@WebServlet(name = "ShopCarDeleteServlet", value = "/shopCarDelete")
public class ShopCarDeleteServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        IShopCarService shopCarService = new ShopCarServiceImpl();
        Integer bookId = Integer.valueOf(req.getParameter("bookId"));
        HttpSession session = req.getSession();
        Customer customer = (Customer) session.getAttribute("customer");
        Integer customerId = customer.getId();

//        调用对应的删除方法
        shopCarService.deleteShopCarByCidAndBId(customerId,bookId);

        //在查询一下新的购物车记录
        List<ShopCar> shopCarList = shopCarService.findShopCarsByCustomer(customerId);
        //如果查询出来的记录为空（删除的是最后一条购物车记录） 可以跳转到主页
        if(shopCarList.size()==0){
            resp.sendRedirect("/index.jsp");
        }else{
            //覆盖原本的购物车session
            session.setAttribute("shopCarList",shopCarList);
            //跳转到购物车页面 重新渲染页面
            resp.sendRedirect("/shopCar.jsp");
        }

    }

}